using UnityEngine;
using QFramework;
using System.Collections.Generic;
using Unity.VisualScripting;
using System;

// 1.请在菜单 编辑器扩展/Namespace Settings 里设置命名空间
// 2.命名空间更改后，生成代码之后，需要把逻辑代码文件（非 Designer）的命名空间手动更改
namespace QFramework.Example
{

    public partial class EnemyGenerator : ViewController
    {
        [SerializeField]
        LevelConfig levelConfig;

        Queue<EnemyWave> enemiesQue = new Queue<EnemyWave>();

        EnemyWave currentWave;

        float float_CreateEnemyTimer = 0;
        float float_WaveTimer = 0;

        int tatilCount = 0;

        bool lastWave => Global.EnemyWaveCount.Value == tatilCount;

        void Start()
        {
            tatilCount = 0;
            foreach (var group in levelConfig.list_EnemyWaveGroups)
            {
                foreach (var item in group.list_EnemyWaves)
                {
                    if (!item.active) continue;
                    enemiesQue.Enqueue(item);
                    tatilCount++;
                }
            }
        }

        void Update()
        {
            if (!Player.Default)
                return;

            if (currentWave == null)
            {
                if (enemiesQue.Count > 0)
                {
                    Global.EnemyWaveCount.Value++;
                    currentWave = enemiesQue.Dequeue();
                    float_CreateEnemyTimer = 0;
                    float_WaveTimer = 0;
                }
            }
            else
            {
                float_CreateEnemyTimer += Time.deltaTime;

                float_WaveTimer += Time.deltaTime;
                if (float_CreateEnemyTimer >= currentWave.generateTime)
                {
                    float_CreateEnemyTimer = 0;

                    // float randomAngle = UnityEngine.Random.Range(0, 361);
                    // float randomRadius = randomAngle * Mathf.Deg2Rad;
                    // Vector3 direction = new Vector3(Mathf.Cos(randomRadius), Mathf.Sin(randomRadius));
                    // Vector3 pos = Player.Default.transform.position + direction * 10;

                    int intPos = RandomUtility.Choose(-1, 1);
                    float posX = 0;
                    float posY = 0;
                    if (intPos == -1)
                    {
                        posX = UnityEngine.Random.Range(CamareController.LFtrans.transform.position.x,
                        CamareController.RItrans.transform.position.x);

                        posY = RandomUtility.Choose(CamareController.LFtrans.transform.position.y,
                        CamareController.RItrans.transform.position.y);
                    }
                    else
                    {
                        posX = RandomUtility.Choose(CamareController.LFtrans.transform.position.x,
                        CamareController.RItrans.transform.position.x);

                        posY = UnityEngine.Random.Range(CamareController.LFtrans.transform.position.y,
                       CamareController.RItrans.transform.position.y);
                    }

                    currentWave.enemy.Instantiate()
                    .Position(new Vector2(posX, posY))
                    .Self((self =>
                    {
                        var enemy = self.GetComponent<IEnemy>();
                        enemy.OnSetHpScale(currentWave.hpScale);
                        enemy.OnSetSpeedScale(currentWave.speedScale);
                    })).Show();
                }
                if (float_WaveTimer >= currentWave.waveTime)
                {
                    currentWave = null;
                }
            }
        }

        /// <summary>
        /// 获取当前是否波次全部结束
        /// </summary>
        /// <returns></returns>
        public bool OnGetWaveIsOver()
        {
            return lastWave;
        }

        /// <summary>
        /// 最后一波是否结束
        /// </summary>
        /// <returns></returns>
        public bool OnGetLastWaveOver()
        {
            return currentWave == null;
        }

        /// <summary>
        /// 重置怪物波次
        /// </summary>
        public void OnRefreshWaveCount()
        {
            Global.EnemyWaveCount.Value = 0;
        }
    }
}
